#include "SdkOsd.h"
#include "bitmapinfo.h"

#define DEF_TAG             "Osd"

#define OSD_LETTER_NUM      20

extern IMPRgnHandle g_SdkVideo_Osd_Hander[FS_CHN_NUM][5];
extern struct chn_conf chn[FS_CHN_NUM];

Com_s32 g_SdkOsd_Status;

int Ext_SdkOsd_Clear(void)
{
    int ret, i;

    for (i = 0; i < FS_CHN_NUM; i++)
    {
        if (chn[i].enable)
        {
            ret = IMP_OSD_ShowRgn(g_SdkVideo_Osd_Hander[i][0], i, 0);
            if (ret != 0)
            {
                PRT_ERR(DEF_TAG, "IMP_OSD_ShowRgn() timeStamp error\n");
                return -1;
            }

            ret = IMP_OSD_ShowRgn(g_SdkVideo_Osd_Hander[i][2], i, 0);
            if (ret != 0)
            {
                PRT_ERR(DEF_TAG, "IMP_OSD_ShowRgn() Logo error\n");
                return -1;
            }
        }
    }

    return 0;
}

int ChipSdk_osd_show(void)
{
    int ret, i;

    for (i = 0; i < FS_CHN_NUM; i++)
    {
        if (chn[i].enable)
        {
            ret = IMP_OSD_ShowRgn(g_SdkVideo_Osd_Hander[i][0], i, 1);
            if (ret != 0)
            {
                PRT_ERR(DEF_TAG, "IMP_OSD_ShowRgn() timeStamp error\n");
                return -1;
            }

            ret = IMP_OSD_ShowRgn(g_SdkVideo_Osd_Hander[i][2], i, 1);
            if (ret != 0)
            {
                PRT_ERR(DEF_TAG, "IMP_OSD_ShowRgn() Logo error\n");
                return -1;
            }
        }
    }

    return 0;
}

void Ext_SdkOsd_SetStatus(Com_s32 flag)
{
    g_SdkOsd_Status = flag;

    if(g_SdkOsd_Status)
    {
        ChipSdk_osd_show();
    }
}

void *ChipSdkOsd_Thread(void* arg)
{
    int ret;
    char DateStr[40];
    time_t currTime;
    struct tm *currDate;
    unsigned i = 0, j = 0;
    void *dateData = NULL;
    char data[OSD_LETTER_NUM * OSD_REGION_HEIGHT * OSD_REGION_WIDTH * sizeof(uint32_t)];
    IMPOSDRgnAttrData rAttrData;

    memset(data,0,sizeof(data));

    while (Ext_InComFile_Access(DEF_FLAG_SYNC_TIME_OK) == DEF_COM_FILE_NOT_EXIST)
    {
        usleep(100 * 1000);
    }

    ret = ChipSdk_osd_show();
    if (ret < 0)
    {
        PRT_ERR(DEF_TAG, "OSD show error\n");
        return NULL;
    }

    DEF_COM_SET_THREAD_NAME("OSD_Thread",g_ComThread_Num);

    while(g_ComThread_Running)
    {
        if (g_SdkOsd_Status == DEF_COM_TRUE)
        {
            int penpos = 0;
            int penpos_t = 0;
            int fontadv = 0;
            unsigned int len = 0;

            time(&currTime);

            currDate = localtime(&currTime);

            memset(DateStr, 0, 40);
            strftime(DateStr, 40, "%Y-%m-%d %H:%M:%S", currDate);
            len = strlen(DateStr);
            for (i = 0; i < len; i++)
            {
                switch(DateStr[i])
                {
                case '0' ... '9':
                    dateData = (void *)gBitmap[DateStr[i] - '0'].pdata;
                    fontadv = gBitmap[DateStr[i] - '0'].width;
                    penpos_t += gBitmap[DateStr[i] - '0'].width;
                    break;

                case '-':
                    dateData = (void *)gBitmap[10].pdata;
                    fontadv = gBitmap[10].width;
                    penpos_t += gBitmap[10].width;
                    break;

                case ' ':
                    dateData = (void *)gBitmap[11].pdata;
                    fontadv = gBitmap[11].width;
                    penpos_t += gBitmap[11].width;
                    break;

                case ':':
                    dateData = (void *)gBitmap[12].pdata;
                    fontadv = gBitmap[12].width;
                    penpos_t += gBitmap[12].width;
                    break;

                default:
                    break;
                }

                for (j = 0; j < gBitmapHight; j++)
                {
                    memcpy((void *)(data + j*OSD_LETTER_NUM*OSD_REGION_WIDTH + penpos),
                           (void *)(dateData + j*fontadv), fontadv);
                }
                penpos = penpos_t;
            }
            rAttrData.bitmapData = data;

            for (i = 0; i < FS_CHN_NUM; i++)
            {
                if (chn[i].enable)
                {
                    IMP_OSD_UpdateRgnAttrData(g_SdkVideo_Osd_Hander[i][0], &rAttrData);
                }
            }
        }
        usleep(500 * 1000);
    }

    DEF_COM_EXIT_THREAD(g_ComThread_Num);
}

void Ext_SdkOsd_Init(void)
{
    pthread_t id;
    pthread_create(&id, NULL, ChipSdkOsd_Thread,NULL);
    pthread_detach(id);
}
